package com.example.mytextview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by jun on 2016/4/18.
 */
public class MyTextView extends View {
    private String mytext;
    private int mycolor;
    private float mysize;
    private Paint mPaint;

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.myTextViewStyle);
        int n = array.getIndexCount();
        mPaint = new Paint();
        for (int i = 0; i < n; i++) {
            int attrname = array.getIndex(i);
            Log.e("wj","attrname =="+attrname);
            switch (attrname) {
                case R.styleable.myTextViewStyle_MyText:
                    Log.e("wj","[myTextViewStyle_MyText] attrname =="+attrname);
                    mytext = array.getString(attrname);
                    break;
                case R.styleable.myTextViewStyle_MyTextColor:
                    Log.e("wj","[myTextViewStyle_MyTextColor] attrname =="+attrname);
                    mycolor = array.getColor(attrname, 0xFFFFFFFF);
                    break;
                case R.styleable.myTextViewStyle_MyTextSize:
                    Log.e("wj","[myTextViewStyle_MyTextSize] attrname =="+attrname);
                    mysize = array.getDimension(attrname, 10);
                    break;
            }

        }
        mPaint.setColor(mycolor);
        Log.e("wj", "mytext ==" + mytext + "mycolor ==" + mycolor + "mysize ==" + mysize);
        array.recycle();

    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(mysize);
        canvas.drawText(mytext,100,200,mPaint);
    }
}
